#!/bin/bash
WORK_PATH=$(cd "$(dirname "$0")";pwd)
GROUP_NAME=$(id -gn)
USER_NAME=$(id -un)
echo "当前用户：$USER_NAME"
echo "当前用户组：$GROUP_NAME"
if [ -n "$acp_user" ]; then
  CUSTOMER_USER=(${acp_user//:/ })
  echo "指定用户ID：${CUSTOMER_USER[0]}"
  echo "指定用户组ID：${CUSTOMER_USER[1]}"
  if [ "$UID" != "${CUSTOMER_USER[0]}" ]; then
    egrep ":${CUSTOMER_USER[1]}:" /etc/group >& /dev/null
    if [ $? -ne 0 ]; then
      groupadd -g ${CUSTOMER_USER[1]} "appgroup"
      GROUP_NAME="appgroup"
    else
      gropinfo=$(egrep "^(.*):${CUSTOMER_USER[1]}:" /etc/group)
      gropsplit=(${gropinfo//:/ })
      GROUP_NAME=${gropsplit[0]}
    fi
    id ${CUSTOMER_USER[0]} >& /dev/null
    if [ $? -ne 0 ]; then
      useradd -u ${CUSTOMER_USER[0]} -g ${CUSTOMER_USER[1]} -m "appuser"
      USER_NAME="appuser"
    else
      userinfo=$(egrep "^(.*):${CUSTOMER_USER[0]}:" /etc/passwd)
      usersplit=(${userinfo//:/ })
      USER_NAME=${usersplit[0]}
    fi
    chown -R $USER_NAME:$GROUP_NAME $WORK_PATH
  fi
fi
su $USER_NAME -s /bin/bash $WORK_PATH/server-img.sh